package com.booklove.controller.voluntary;

import com.booklove.constant.VoluntaryworkStateConstant;
import com.booklove.context.BaseContext;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.query.VoluntaryworkQuery;
import com.booklove.domain.vo.VoluntaryworkVO;
import com.booklove.result.Result;
import com.booklove.service.delivery.DeliverService;
import com.booklove.service.voluntary.VoluntaryworkService;
import com.booklove.domain.po.Voluntarywork;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@Slf4j
@RestController("voluntaryworkCLL")
@RequestMapping("/voluntarywork")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class VoluntaryworkController {

    private final VoluntaryworkService voluntaryworkService;
    private final DeliverService deliverServicel;

    /**
     * 保存新增
     */
//    @PostMapping("/")
//    //@AutoFill(OperationType.INSERT)
//    Result<Voluntarywork> saveVoluntarywork(@RequestBody VoluntaryworkFormDTO voluntaryworkFormDTO) {
//        Deliver deliver=new Deliver();
//        BeanUtils.copyProperties(voluntaryworkFormDTO,deliver);
//        deliverServicel.save(deliver);
//        Integer deliverId = deliver.getId();
//
//        Voluntarywork voluntarywork = new Voluntarywork();
//        BeanUtils.copyProperties(voluntaryworkFormDTO, voluntarywork);
//        voluntarywork.setDeliverId(deliverId);
//
//        voluntarywork.setCreatedTime(LocalDateTime.now());
//        voluntarywork.setUpdatedTime(LocalDateTime.now());
//        voluntarywork.setCreatedBy(BaseContext.getCurrentId().intValue());
//        voluntarywork.setUpdatedBy(BaseContext.getCurrentId().intValue());
//
//        voluntaryworkService.save(voluntarywork);
//
//        return Result.success(voluntarywork);
//
//    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteVoluntarywork(@RequestParam("id") Long id) {
        voluntaryworkService.removeById(id);
        return Result.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    VoluntaryworkVO getVoluntarywork(@PathVariable Long id) {
        Voluntarywork voluntarywork = voluntaryworkService.getById(id);
        VoluntaryworkVO voluntaryworkVO=new VoluntaryworkVO();
        BeanUtils.copyProperties(voluntarywork,voluntaryworkVO);
        return voluntaryworkVO;
    }


    /**
     * 编辑数据
     *
     * @param voluntaryworkFormDTO 实体
     * @return 编辑结果
     */
//    @PutMapping("/")
//    //@AutoFill(OperationType.UPDATE)
//    Result<String> updateVoluntarywork(@RequestBody VoluntaryworkFormDTO voluntaryworkFormDTO) {
//        Voluntarywork voluntarywork=new Voluntarywork();
//        BeanUtils.copyProperties(voluntaryworkFormDTO,voluntarywork);
//        voluntarywork.setUpdatedTime(LocalDateTime.now());
//        voluntarywork.setUpdatedBy(BaseContext.getCurrentId().intValue());
//        voluntaryworkService.updateById(voluntarywork);
//        return Result.success();
//    }

    /**
     * 根据id确认书籍接受
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    Result<String> accpet(@PathVariable Integer id) {
        Voluntarywork voluntarywork=voluntaryworkService.getById(id);
        voluntarywork.setVoluntaryWorkState(VoluntaryworkStateConstant.ACCEPT);
        voluntarywork.setUpdatedTime(LocalDateTime.now());
        voluntarywork.setUpdatedBy(BaseContext.getCurrentId().intValue());
        voluntaryworkService.updateById(voluntarywork);
        return Result.success();
    }




    /**
     * 分页查询
     *
     * @param voluntaryworkQuery 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<VoluntaryworkVO> voluntaryworksPageQuery(VoluntaryworkQuery voluntaryworkQuery) {
        return voluntaryworkService.queryVoluntaryworksPage(voluntaryworkQuery);
    }

}

